package com.cn.yunhe.rentcars.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.CarMapper;
import com.cn.yunhe.rentcars.mapper.CarOrderMapper;
import com.cn.yunhe.rentcars.mapper.CarPicMapper;
import com.cn.yunhe.rentcars.mapper.CarPriceMapper;
import com.cn.yunhe.rentcars.mapper.ClientUserMapper;
import com.cn.yunhe.rentcars.mapper.CreditMapper;
import com.cn.yunhe.rentcars.mapper.MemberLevelMapper;
import com.cn.yunhe.rentcars.model.Car;
import com.cn.yunhe.rentcars.model.CarOrder;
import com.cn.yunhe.rentcars.model.CarPic;
import com.cn.yunhe.rentcars.model.CarPrice;
import com.cn.yunhe.rentcars.model.ClientUser;
import com.cn.yunhe.rentcars.model.Credit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @author 吴银月 我要还车模块：还车登记
 */
@RestController
public class ReturnCarController {

	@Autowired
	private CarOrderMapper carOrderMapper;
	@Autowired
	private CarMapper carMapper;
	@Autowired
	private CarPriceMapper carPriceMapper;
	@Autowired
	private ClientUserMapper clientUserMapper;
	@Autowired
	private MemberLevelMapper memberLevelMapper;
	@Autowired
	private BusiLogMapper busiLogMapper;
	@Autowired
	private CreditMapper creditMapper;
	@Autowired
	private CarPicMapper carPicMapper;

	/**
	 * TODO：车辆列表 吴银月
	 */
	@RequestMapping(value = "/carquery", method = RequestMethod.GET)
	public ModelAndView carquery() {
		return new ModelAndView("car-query");
	}

	/**
	 * TODO：租用历史 吴银月
	 */
	@RequestMapping(value = "/renthistory", method = RequestMethod.GET)
	public ModelAndView renthistory() {
		return new ModelAndView("rent-history");
	}

	/**
	 * TODO：还车列表 吴银月
	 */

	@RequestMapping(value = "/carorderlist", method = RequestMethod.GET)
	public ModelAndView carOrderList() {
		return new ModelAndView("order-list");
	}

	/**
	 * TODO：我要还车 吴银月
	 */
	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	public ModelAndView OrderList(@RequestParam(required = false) String orderid) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (orderid != null) {
			if (carOrderMapper.queryOrderId().contains(orderid)) {
				CarOrder obj = carOrderMapper.getOrdersById(orderid);
				Car car = carMapper.getCarByCarid(obj.getCarid());
				CarPrice carPrice = carPriceMapper.getCarPriceById(obj.getCarid());
				int day = carOrderMapper.prerentday(orderid);
				double sumMoney = carOrderMapper.moneysum(orderid);
				double readyMoney = carOrderMapper.readymoneysum(orderid);
				List<CarPic> pic = carPicMapper.readPicName(obj.getCarid());
				if (obj != null) {
					modelMap.put("info", obj);
					modelMap.put("car", car);
					modelMap.put("carPrice", carPrice);
					modelMap.put("day", day);
					modelMap.put("sumMoney", sumMoney);
					modelMap.put("readyMoney", readyMoney);
					if (pic.size() == 0) {
						modelMap.put("picname", "1.jpg");
						modelMap.put("picurl", "images");
					} else {
						modelMap.put("picname", pic.get(0).getPicName());
						modelMap.put("picurl", pic.get(0).getPicUrl());
					}
				}
			} else {
				modelMap.put("worn", "没有此订单/此车已还，请重新输入或者等待审核！");
			}
		}

		return new ModelAndView("return-edit", modelMap);
	}

	/**
	 * TODO：我要还车 业务编辑 吴银月
	 */
	@RequestMapping(value = "/orderlisttwo", method = RequestMethod.GET)
	public ModelAndView OrderListTwo(
			@RequestParam(required = false) String orderid) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (orderid != null) {
			CarOrder obj = carOrderMapper.getOrdersById(orderid);
			Car car = carMapper.getCarByCarid(obj.getCarid());
			CarPrice carPrice = carPriceMapper.getCarPriceById(obj.getCarid());
			int day = carOrderMapper.prerentday(orderid);
			double sumMoney = carOrderMapper.moneysum(orderid);
			double readyMoney = carOrderMapper.readymoneysum(orderid);
			List<CarPic> pic = carPicMapper.readPicName(obj.getCarid());
			if (obj != null) {
				modelMap.put("info", obj);
				modelMap.put("car", car);
				modelMap.put("carPrice", carPrice);
				modelMap.put("day", day);
				modelMap.put("sumMoney", sumMoney);
				modelMap.put("readyMoney", readyMoney);
				if (pic.size() == 0) {
					modelMap.put("picname", "1.jpg");
					modelMap.put("picurl", "images");
				} else {
					modelMap.put("picname", pic.get(0).getPicName());
					modelMap.put("picurl", pic.get(0).getPicUrl());
				}
			}
		}
		return new ModelAndView("return-edit-two", modelMap);
	}

	/**
	 * TODO：审核 业务编辑 吴银月
	 */
	@RequestMapping(value = "/ordercheck", method = RequestMethod.GET)
	public ModelAndView OrderCheck(@RequestParam String orderid) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (orderid != null) {
			CarOrder obj = carOrderMapper.getOrdersById(orderid);
			Car car = carMapper.getCarByCarid(obj.getCarid());
			CarPrice carPrice = carPriceMapper.getCarPriceById(obj.getCarid());
			int day = carOrderMapper.realrentday(orderid);
			double sumMoney = carOrderMapper.moneysum(orderid);
			double readyMoney = carOrderMapper.readymoneysum(orderid);
			List<CarPic> pic = carPicMapper.readPicName(obj.getCarid());
			ClientUser client = clientUserMapper.querybyuid(obj.getClientid());
			if (obj != null) {
				modelMap.put("info", obj);
				modelMap.put("car", car);
				modelMap.put("carPrice", carPrice);
				modelMap.put("day", day);
				modelMap.put("sumMoney", sumMoney);
				modelMap.put("readyMoney", readyMoney);
				modelMap.put("client", client);
				if (pic.size() == 0) {
					modelMap.put("picname", "1.jpg");
					modelMap.put("picurl", "images");
				} else {
					modelMap.put("picname", pic.get(0).getPicName());
					modelMap.put("picurl", pic.get(0).getPicUrl());
				}
			}
		}
		return new ModelAndView("check", modelMap);
	}

	/**
	 * TODO：租车历史 查看明细 吴银月
	 */
	@RequestMapping(value = "/historyquery", method = RequestMethod.GET)
	public ModelAndView HistoryQuery(
			@RequestParam(required = false) String orderid) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if (orderid != null) {
			CarOrder obj = carOrderMapper.getOrdersById(orderid);
			Car car = carMapper.getCarByCarid(obj.getCarid());
			CarPrice carPrice = carPriceMapper.getCarPriceById(obj.getCarid());
			int day = carOrderMapper.realrentday(orderid);
			double sumMoney = carOrderMapper.moneysum(orderid);
			double readyMoney = carOrderMapper.readymoneysum(orderid);
			List<CarPic> pic = carPicMapper.readPicName(obj.getCarid());
			if (obj != null) {
				modelMap.put("info", obj);
				modelMap.put("car", car);
				modelMap.put("carPrice", carPrice);
				modelMap.put("day", day);
				modelMap.put("sumMoney", sumMoney);
				modelMap.put("readyMoney", readyMoney);
				if (pic.size() == 0) {
					modelMap.put("picname", "1.jpg");
					modelMap.put("picurl", "images");
				} else {
					modelMap.put("picname", pic.get(0).getPicName());
					modelMap.put("picurl", pic.get(0).getPicUrl());
				}
			}
		}
		return new ModelAndView("history-query", modelMap);
	}

	/**
	 * TODO：还车登记 修改订单 吴银月
	 * 
	 * @param orderid
	 */

	@RequestMapping(value = "/orders/{orderid}", method = RequestMethod.PUT)
	public void orderEdit(@PathVariable("orderid") String orderid,
			@RequestBody CarOrder carorder, HttpServletRequest request) {
		CarOrder co = carOrderMapper.getOrdersById(orderid);
		co.setReturntimeout(carorder.getReturntimeout());
		co.setReturndamage(carorder.getReturndamage());
		co.setRealettime(carorder.getRealettime());
		co.setRealetoil(carorder.getRealetoil());
		co.setRealetmile(carorder.getRealetmile());
		co.setOrderstate(carorder.getOrderstate());
		Car car = carMapper.getCarByCarid(carorder.getCarid());
		car.setCarstate(carorder.getCarstate());
		carOrderMapper.editOrder(co);
		carMapper.editState(car);
		// 添加日志信息
		Map<String, Object> busMap = new HashMap<String, Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "修改还车订单信息");
		busMap.put("busitype", "修改");
		busMap.put("busiconent", "修改还车订单信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
	}

	/**
	 * TODO：审核订单 吴银月
	 * 
	 * @param orderid
	 */
	@RequestMapping(value = "/orderstwo/{orderid}", method = RequestMethod.PUT)
	public void orderEditTwo(@PathVariable("orderid") String orderid,
			@RequestBody CarOrder carorder, HttpServletRequest request) {
		CarOrder co = carOrderMapper.getOrdersById(orderid);
		co.setReturntimeout(carorder.getReturntimeout());
		co.setReturndamage(carorder.getReturndamage());
		co.setReturnillegal(carorder.getReturnillegal());
		co.setOrderstate(carorder.getOrderstate());
		//System.out.println(carorder.getCreditnum());
		int creditnum = carorder.getCreditnum() + 3;
		System.out.println(carorder.getUid());
		Credit credit = new Credit();
		if (carorder.getReturnillegal() == 0) {
			creditnum += 3;
			credit.setCid(carorder.getUid());
			credit.setChangereason("正常租车");
			credit.setCreditchange("+3");
			creditMapper.addCredit(credit);
		} else if (carorder.getReturnillegal() > 0
				&& carorder.getReturnillegal() <= 200) {
			creditnum += 2;
			credit.setCid(carorder.getUid());
			credit.setChangereason("正常租车，违章少");
			credit.setCreditchange("+2");
			creditMapper.addCredit(credit);
		} else if (carorder.getReturnillegal() > 200
				&& carorder.getReturnillegal() <= 400) {
			creditnum += 1;
			credit.setCid(carorder.getUid());
			credit.setChangereason("正常租车,违章较多");
			credit.setCreditchange("+1");
			creditMapper.addCredit(credit);
		} else {
			creditnum -= 3;
			credit.setCid(carorder.getUid());
			credit.setChangereason("正常租车,违章较多");
			credit.setCreditchange("-3");
			creditMapper.addCredit(credit);
		}
		// 修改信用分值
		ClientUser clientUser = clientUserMapper.querybyuid(co.getClientid());
		clientUser.setCreditnum(creditnum);
		// 修改信用等级
		String str = clientUserMapper.queryScore(co.getClientid());
		clientUser.setMemlevel(str);
		carOrderMapper.editOrder(co);
		clientUserMapper.editClient(clientUser);
		// 添加日志信息
		Map<String, Object> busMap = new HashMap<String, Object>();
		busMap.put("logid", UUID.randomUUID().toString());
		busMap.put("businame", "修改还车订单信息");
		busMap.put("busitype", "修改");
		busMap.put("busiconent", "修改还车订单信息");
		BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		System.out.println("haichechenggong");
	}

	/**
	 * 搜索查询订单表所有状态为3(待还车),4(已还车)的订单 吴银月
	 * 
	 * @param orderid
	 * @param mobile
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */

	@RequestMapping(value = "/carorders", method = RequestMethod.GET)
	public Map<String, Object> getLikeOrders(
			@RequestParam(required = false) String mobile,
			@RequestParam(required = false) String orderid,
			@RequestParam int draw, @RequestParam int start,
			@RequestParam int length) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("orderid", orderid);
		params.put("mobile", mobile);

		int pageNum = (start / length) + 1;
		int pageSize = length;

		PageHelper.startPage(pageNum, pageSize);
		List<CarOrder> orderList = carOrderMapper.searchReturn(params);
		PageInfo<CarOrder> page = new PageInfo<CarOrder>(orderList);

		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("draw", draw);
		rsMap.put("data", page.getList());
		rsMap.put("recordsTotal", page.getTotal());
		rsMap.put("recordsFiltered", page.getTotal());
		return rsMap;
	}

	/**
	 * 搜索查询订单表所有状态为5(已完毕)的订单 吴银月
	 * 
	 * @param carid
	 * @param draw
	 * @param start
	 * @param length
	 * @return
	 */

	@RequestMapping(value = "/ordershistory", method = RequestMethod.GET)
	public Map<String, Object> getOrdersHistory(@RequestParam String carid,
			@RequestParam String datemin, @RequestParam String datemax,
			@RequestParam int draw, @RequestParam int start,
			@RequestParam int length) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("carid", carid);
		params.put("datemin", datemin);
		params.put("datemax", datemax);

		int pageNum = (start / length) + 1;
		int pageSize = length;

		PageHelper.startPage(pageNum, pageSize);
		List<CarOrder> orderList = carOrderMapper.searchhistory(params);
		PageInfo<CarOrder> page = new PageInfo<CarOrder>(orderList);

		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("draw", draw);
		rsMap.put("data", page.getList());
		rsMap.put("recordsTotal", page.getTotal());
		rsMap.put("recordsFiltered", page.getTotal());
		return rsMap;
	}

}
